#include "MyTileSources.h"
#include <QCoreApplication>
#include <QMutex>
#include <QDir>
#include "MyScene.h"
#include "MyView.h"
#include "MyTileGraphicsItem.h"
#include "MyMap_global.h"
#include "TileKey.h"



QMutex mutex;
MyTileSources::MyTileSources(MyScene * pscene ,MyView * pview):
	m_pHttpLoader(NULL)
{
	m_pScne = pscene;
	m_pView = pview;
}


MyTileSources::~MyTileSources()
{
}

void MyTileSources::RequstData(int x, int y, int z, MyTileGraphicsItem* item,MapDataType& maptype)
{
	if (item == NULL)
	{
		return;
	}
	QString filePath, filename, url;
	GetTileFileName(x, y, z, filePath, filename, maptype);
	url = GetDownLoadURL(x, y, z, maptype);
	QDir dir;
	if (!dir.exists(filePath))
	{
		bool res = dir.mkpath(filePath);
	}
	QFile file(filePath + "/" + filename);
	if (file.exists())
	{
		if (file.size() == 0)
		{
			file.remove();
			shared_ptr<HttpLoader> ptrloader(new HttpLoader());
			ptrloader->SetTileSource(this);
			map[filePath + "/" + filename] = ptrloader;
			itemmap[filePath + "/" + filename] = item;
			ptrloader.get()->downloadFileFromURL(url, filePath + "/" + filename);
			return;
		}
		QString path = filePath + "/" + filename;
		item->SetPath(path);
		item->m_bIsLoaded = true;
	}
	else
	{
		shared_ptr<HttpLoader> ptrloader(new HttpLoader());
		ptrloader->SetTileSource(this);
		//loader->SetTileSource(this);
		map[filePath + "/" + filename] = ptrloader;
		itemmap[filePath + "/" + filename] = item;
		QString path(filePath + "/" + filename);
		ptrloader.get()->downloadFileFromURL(url, filePath + "/" + filename);
	}
}

void MyTileSources::Clear()
{


}

void MyTileSources::UpateSource()
{
	
}

void MyTileSources::GetTileFileName(int x, int y, int z, QString & pathfile, QString & filename, MapDataType& maptype)
{
	switch (maptype )
	{
	case  MapDataType::ImageNotes:
		pathfile = QCoreApplication::applicationDirPath() + "/../../MapData/GoogleImageTile/Notes/" + QString("%1/").arg(z) + QString("%1").arg(x);
		filename = QString("%1").arg(y) + ".jpg";
		break;
	case  MapDataType::ImageWithoutNotes:
		pathfile = QCoreApplication::applicationDirPath() + "/../../MapData/GoogleImageTile/NoNotes/" + QString("%1/").arg(z) + QString("%1").arg(x);
		filename = QString("%1").arg(y) + ".jpg";
	    break;
	case  MapDataType::VectorTile:
	
		pathfile = QCoreApplication::applicationDirPath() + "/../../MapData/GoogleVectorTile/" + QString("%1/").arg(z) + QString("%1").arg(x);
		filename = QString("%1").arg(y) + ".png";
	    break;
	case  MapDataType::TerrainWithoutNotes:
		pathfile = QCoreApplication::applicationDirPath() + "/../../MapData/GoogleTerrainTile/NoNotes/" + QString("%1/").arg(z) + QString("%1").arg(x);
		filename = QString("%1").arg(y) + ".jpg";
	    break;
	case  MapDataType::TerrainNotes:
		pathfile = QCoreApplication::applicationDirPath() + "/../../MapData/GoogleTerrainTile/Notes/" + QString("%1/").arg(z) + QString("%1").arg(x);
		filename = QString("%1").arg(y) + ".jpg";
		break;
	default:
		break;
	}
	
	
}

QString MyTileSources::GetDownLoadURL(int x, int y, int z, MapDataType & maptype)
{
	switch (maptype)
	{
	case  MapDataType::ImageNotes:
		return  QString(ImageNotesURL).arg(x).arg(y).arg(z);
	case  MapDataType::ImageWithoutNotes:
		return  QString(ImageNoNotesURL).arg(x).arg(y).arg(z);
	case  MapDataType::VectorTile:
		return  QString(VecotorURL).arg(x).arg(y).arg(z);
	case  MapDataType::TerrainWithoutNotes:
		return  QString(terrainURL).arg(x).arg(y).arg(z);
	case  MapDataType::TerrainNotes:
		return  QString(terrainWithNotesURL).arg(x).arg(y).arg(z);
	default:
		return QString(ImageNotesURL).arg(x).arg(y).arg(z);
	}
}

void MyTileSources::slot_returResult(HttpLoader * loader, QString & path)
{

	if (map[path]!=NULL)
	{
		itemmap[path]->SetPath(path);
		 mutex.lock();
		 QMap<QString, shared_ptr<HttpLoader>>::iterator it= map.find(path);
		 map.erase(it);
		mutex.unlock();
	}
	m_pScne->update();
}
